﻿@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.ClientStore;
@using SimpleIdServer.IdServer.Website.Stores.UserStore;
@inherits Fluxor.Blazor.Web.Components.FluxorLayout
@inject NotificationService notificationService
@inject NavigationManager navigationManager
@inject ContextMenuService contextMenuService
@inject DialogService dialogService
@layout MainLayout
@inject IState<ClientKeysState> clientKeysState
@inject IDispatcher dispatcher

<div>
    <p class="text-muted">@Global.UseJwksUrlHelper</p>
    <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.UseJwksUrl</RadzenText>
    <RadzenSwitch Change="HandleUseJWKSUrl" @bind-Value=@isJWKSUrlUsed></RadzenSwitch>
    <RadzenTemplateForm Submit=@UpdateJWKSUrl TItem="UpdateJWKSUrlForm" Data=@updateJWKSUrl>
        <div>
            <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.Url</RadzenText>
            <RadzenTextBox Name="JWKSUrl" @bind-Value="@updateJWKSUrl.Url" Disabled=@(!isJWKSUrlUsed) Class="w-100"></RadzenTextBox>
            <RadzenRequiredValidator Component="JWKSUrl" Text="@Global.UrlRequired"></RadzenRequiredValidator>
        </div>
        <SidAuthorizeView Roles=@("/clients/manage")>
            <Authorized>
                <RadzenButton class="mt-1" Variant="Variant.Flat" ButtonType="ButtonType.Submit" ButtonStyle="ButtonStyle.Success" Text="@Global.Update" Disabled=@(!isJWKSUrlUsed) />
            </Authorized>
        </SidAuthorizeView>
    </RadzenTemplateForm>
</div>

<hr />

<div>
    <SidAuthorizeView Roles=@("/clients/manage")>
        <Authorized>
            <RadzenButton class="mb-1" Click="@(args => GenerateKey())" Icon="add" Variant="Variant.Flat" ButtonStyle="ButtonStyle.Primary" Size="ButtonSize.Medium" Text="@Global.GenerateNewKey" />
        </Authorized>
    </SidAuthorizeView>

    <RadzenDataGrid AllowFiltering="true"
                    AllowColumnResize="true"
                    AllowAlternatingRows="false"
                    AllowSorting="true"
                    PageSize="30"
                    AllowPaging="true"
                    PagerHorizontalAlign="HorizontalAlign.Left"
                    ShowPagingSummary="true"
                    IsLoading="@clientKeysState.Value.IsLoading"
                    Count="@clientKeysState.Value.Count"
                    Data="@clientKeysState.Value.Keys"
                    RowRender="@RowRender"
                    TItem="SelectableClientKey"
                    ColumnWidth="300px">
        <Columns>
            <RadzenDataGridColumn TItem="SelectableClientKey" Filterable="false" Sortable="false" Width="80px" TextAlign="TextAlign.Center">
                <HeaderTemplate>
                    <RadzenCheckBox @bind-Value=@selectAll Change="@(args => ToggleAll(args))" TValue="bool" />
                </HeaderTemplate>
                <Template Context="data">
                    <RadzenCheckBox @bind-Value=@data.IsSelected Change="@(args => ToggleChanged(args, data))" TValue="bool" />
                </Template>
            </RadzenDataGridColumn>
            <RadzenDataGridColumn TItem="SelectableClientKey" Property="Value.Kid" Filterable="false" Sortable="true" SortOrder=SortOrder.Ascending Title="@Global.Identifier" Width="80px" />
            <RadzenDataGridColumn TItem="SelectableClientKey" Property="Value.Alg" Filterable="false" Title="@Global.Algorithm" Width="80px" />
            <RadzenDataGridColumn TItem="SelectableClientKey" Filterable="false" Title="@Global.Usage" Width="80px">
                <Template Context="data">
                    <RadzenBadge Text="@data.Value.Usage" IsPill="true" />
                </Template>
            </RadzenDataGridColumn>
            <RadzenDataGridColumn TItem="SelectableClientKey" Filterable="false" Sortable="false" FormatString="{0:dd/M/yyyy HH:mm:ss}" Width="80px" TextAlign="TextAlign.Center">
                <Template Context="data">µ
                    <SidAuthorizeView Roles=@("/clients/manage")>
                        <Authorized>
                            <RadzenButton Icon="more_vert" Click="@(args => ShowMoreContextMenu(data, args))" />                        
                        </Authorized>
                    </SidAuthorizeView>
                </Template>
            </RadzenDataGridColumn>
        </Columns>
    </RadzenDataGrid>
</div>

@code {
    bool selectAll;
    bool isJWKSUrlUsed { get; set; } = false;

    [Parameter]
    public SimpleIdServer.IdServer.Domains.Client Client { get; set; }

    record UpdateJWKSUrlForm
    {
        public string? Url { get; set; } = null;
    }

    UpdateJWKSUrlForm updateJWKSUrl = new UpdateJWKSUrlForm();

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        if (firstRender)
        {
            SubscribeToAction<RemoveSelectedClientKeysSuccessAction>((act) =>
            {
                notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = Global.SelectedClientKeysRemoved });
                StateHasChanged();
            });
            SubscribeToAction<UpdateJWKSUrlSuccessAction>((act) =>
            {
                notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = Global.JWKSUrlUpdated });
                StateHasChanged();
            });
        }
    }

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
        if(Client == null) return;
        isJWKSUrlUsed = !string.IsNullOrWhiteSpace(Client.JwksUri);
        updateJWKSUrl.Url = Client.JwksUri;
    }


    void ShowMoreContextMenu(SelectableClientKey scope, MouseEventArgs args)
    {
        contextMenuService.Open(args, new List<ContextMenuItem>
        {
            new ContextMenuItem { Text = Global.Delete, Value = 1 }
        }, (a) =>
    {
    if (a.Value.Equals(1))
    {
        var keyIds = clientKeysState.Value.Keys.Where(s => s.IsSelected).Select(s => s.Value.Kid).ToList();
        var act = new RemoveSelectedClientKeysAction { KeyIds = keyIds, ClientId = Client.ClientId };
        dispatcher.Dispatch(act);
        contextMenuService.Close();
    }
    });
    }

    void ToggleChanged(bool isSelected, SelectableClientKey key)
    {
        var act = new ToggleClientKeySelectionAction { IsSelected = isSelected, KeyId = key.Value.Kid };
        dispatcher.Dispatch(act);
    }


    void ToggleAll(bool isSelected)
    {
        var act = new ToggleAllClientKeySelectionAction { IsSelected = isSelected };
        dispatcher.Dispatch(act);
    }

    void RowRender(RowRenderEventArgs<SelectableClientKey> row)
    {
        const string className = "class";
        if (row.Data.IsNew)
            row.Attributes.Add(className, "new");
        else if (row.Data.IsSelected)
            row.Attributes.Add(className, "active");
        else if (row.Attributes.ContainsKey(className))
            row.Attributes.Remove(className);
    }

    async void GenerateKey()
    {
        await dialogService.OpenAsync<GenerateKeyDialog>(Global.GenerateNewKey, new Dictionary<string, object>
        {
            { "Client", Client }
        }, new DialogOptions
        {
            Width = "900px",
            Height = "512px",
            Resizable = true,
            Draggable = true
        });
    }

    void UpdateJWKSUrl(UpdateJWKSUrlForm form)
    {
        dispatcher.Dispatch(new UpdateJWKSUrlAction { ClientId = Client.ClientId, JWKSUrl = form.Url, Client = this.Client });
    }

    void HandleUseJWKSUrl(bool isEnabled)
    {
        
    }
}